<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2022-2099 李志刚（854378082@qq.com）
 * @Date: 2020-02-29 08:53:47
 * @Description: 友情链接管理
 * @LastEditors: 李志刚
 * @LastEditTime: 2022-04-30 09:27:12
 * @FilePath: /CoinCMF/app/Http/Controllers/Console/Content/LinkController.php
 */

declare(strict_types=1);

namespace App\Http\Controllers\Console\Content;

use App\Models\Content\Link;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\BatchRequest;
use App\Http\Requests\Cms\LinkRequest;
use App\Http\Requests\Cms\LinkUpdateRequest;
use App\Http\Requests\DetailRequest;
use App\Http\Requests\SortRequest;


class LinkController extends Controller
{
    public function getList(Request $request)
    {
        $key = $request->input('key', '');
        $page = $request->input('page', 1);
        $size = $request->input('size', 10);
        $query = Link::where(function ($q) use ($key) {
            if ($key != '') {
                $q->where('title', 'like', '%' . $key . '%');
            }
        })->where('del_flag', 0);
        $count = $query->count();
        $list = $query->limit($size)->offset(($page - 1) * $size)->orderBy('sort', 'desc')->orderBy('id', 'desc')->get();
        $res = ['list' => $list, 'count' => $count];
        return $this->resJson(200, '成功', $res);
    }
    public function postCreate(LinkRequest $request)
    {
        $data = $request->all();
        $create = ['title' => $data['title'], 'thumb' => $data['thumb'], 'url' => $data['url'], 'sort' => $data['sort'], 'status' => $data['status']];
        Link::create($create);
        return $this->resJson(200, '成功');
    }
    public function getDetail(DetailRequest $request)
    {
        $detail_id = $request->input('detail_id');
        $detail = Link::findOrFail($detail_id);
        return $this->resJson(200, '成功', $detail);
    }
    public function postEdit(LinkUpdateRequest $request)
    {
        $detail_id = $request->input('detail_id');
        $data = $request->all();
        $update = ['title' => $data['title'], 'thumb' => $data['thumb'], 'url' => $data['url'], 'sort' => $data['sort'], 'status' => $data['status']];
        Link::where('id', $detail_id)->update($update);
        return $this->resJson(200, '成功');
    }
    public function postRemove(BatchRequest $request)
    {
        $detail_id = $request->input('detail_id',[]);
        Link::whereIn('id', $detail_id)->update(['del_flag'=>1]);
        return $this->resJson(200, '完成');
    }
    public function postSort(SortRequest $request)
    {
        $detail_id = $request->input('detail_id');
        Link::where('id', $detail_id)->update(['sort'=>$request->input('sort',0)]);
        return $this->resJson(200, '成功');
    }
}
